<?php

class ReviewService extends AbstractService
{

    protected function _init()
    {
	$this->setMapper(new ReviewMapper());
    }

    public function getReviewList($params = array(), $page = 1, $limit = 10)
    {
	$params['limit'] = $limit;
	$params['offset'] = ($page - 1) * $limit;
	$cacheSlot = new CacheSlotsReviewList($params);
	if (($data = $cacheSlot->load()) === false) {
	    $data = $this->getMapper()->getReviewList($params);
	    $cacheSlot->setData($data)->save();
	}
	return $data;
    }

    public function getReviewListByCategory(CategoryModel $category, $page = 1, $limit = 10, $params = array())
    {
	if (!$category->getId()) {
	    return false;
	}

	$params['where'] = 'category_id = :category_id';
	$params['params'] = array(':category_id' => $category->getId());
	return $this->getReviewList($params, $page, $limit);
    }

    public function getReviewCountByCategory($category)
    {
	if (!$category->getId()) {
	    return false;
	}
	return $this->getCount(array(
		    "where" => "category_id = :category_id",
		    "params" => array(":category_id" => $category->getId())
			)
	);
    }

    public function getReviewCategory(ReviewModel $model)
    {
	$categoryId = $model->getDbData('category_id');
	if (!$categoryId) {
	    return false;
	}
	$category = FactoryService::get('category')->get($categoryId);
	if ($category) {
	    $model->setCategory($category);
	}
    }

    public function getCountByUser(UserModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	return $this->getCount(array('where' => 'user_id = :user_id', 'params' => array(':user_id' => $model->getId())));
    }

    public function getReviewUser(ReviewModel $model)
    {
	$userId = $model->getDbData('user_id');

	if (!$userId) {
	    return false;
	}

	$user = FactoryService::get('user')->get($userId);

	if ($user->getId()) {
	    $model->setUser($user);
	}
    }

    public function getByUser(UserModel $model, $page = 1, $limit = 10)
    {
	if (!$model->getId()) {
	    return false;
	}

	$cacheSlot = new CacheSlotsUserFeatures($model, 'reviews', array('page' => $page, 'limit' => $limit));
	if (($reviews = $cacheSlot->load()) === false) {
	    $reviews = $this->getMapper()->getByUserId($model->getId(), $page, $limit);
	    $cacheSlot->setData($reviews)->save();
	}
	$model->setReviews($reviews);
    }

    public function getExtendedData(ReviewModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	FactoryService::get('image')->getByReview($model);
	FactoryService::get('map')->getByReview($model);
	FactoryService::get('comment')->getByReview($model);
	FactoryService::get('video')->getByReview($model);
	FactoryService::get('opinion')->getByReview($model);
	$this->getReviewCategory($model);
	$this->getReviewUser($model);
	$this->getSimilarModels($model);
    }

    public function getSimilarModels(ReviewModel $model)
    {
	if (!$model->getDbData('title')) {
	    return false;
	}
	$cacheSlot = new CacheSlotsReviewList(array('id' => $model->getId(), 'type' => 'similar'));
	if (($models = $cacheSlot->load()) === false) {
	    $models = $this->getMapper()->getSimilarModels($model->getId(), $model->getDbData('title'));
	    if (!$models) {
		$models = array();
	    }
	    $cacheSlot->setData($models)->save();
	}
	$model->setSimilarModels($models);
    }

    public function onAfterSave(AbstractModel $model = null)
    {
	if ($model->getId()) {
	    $cacheSlot = new CacheSlotsPk($this->getMapper()->getTableName(), $model->getId());
	    $cacheSlot->delete();
	    $cacheSlot = new CacheSlotsReviewFeatures($model, 'images');
	    $cacheSlot->delete();
	    $cacheSlot = new CacheSlotsReviewFeatures($model, 'maps');
	    $cacheSlot->delete();
	    $cacheSlot = new CacheSlotsReviewFeatures($model, 'videos');
	    $cacheSlot->delete();
	    FactoryService::get('search')->toIndex(array(
		'id' => 'review' . $model->getId(),
		'type' => 'review',
		'url' => Yii::app()->createUrl('review/view', array('id' => $model->getId(), 'url' => $model->getDbData('url'))),
		'text' => implode('. ', array($model->getDbData('title'), $model->getDbData('content'))),
		'title' => $model->getDbData('title')
	    ));
	}
	CacheManager::getCache('memcache')->clear('all.review');
	return true;
    }

    public function onAfterRemove(AbstractModel $model)
    {
	if (parent::onAfterRemove($model)) {
	    CacheManager::getCache('memcache')->clear('all.review');
	}
	return true;
    }

}